﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EHTWebsite.Code;
using System.Net;
using System.Net.Mail;
using System.Web.Security;
using System.Web.Profile;

namespace EHTWebsiteProject
{
    public partial class ResetPass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnResetPass_Click(object sender, EventArgs e)
        {
            MembershipUser theUser = Membership.GetUser(this.txtEmail.Text.Trim());
            ProfileBase theProfile = ProfileBase.Create(this.txtEmail.Text.Trim());
            theUser.ResetPassword();

            ThongTinCongTy theThongTinCongTy = ThongTinCongTyProvider.GetThongTinCongTyByCongTyID(1);
            String messageSendMail = "Send mail suksess";//Send mail success
            try
            {
                SmtpClient SmtpServer = new SmtpClient();
                SmtpServer.Credentials = new System.Net.NetworkCredential(theThongTinCongTy.Email, theThongTinCongTy.EmailPass);
                Boolean yahoovn = theThongTinCongTy.Email.Contains("yahoo.com.vn");
                Boolean yahoo = theThongTinCongTy.Email.Contains("yahoo.com");
                Boolean gmail = theThongTinCongTy.Email.Contains("gmail.com");
                if (yahoo == true)
                {
                    SmtpServer.Host = "smtp.mail.yahoo.com";
                    SmtpServer.Port = 587;
                }
                else if (yahoovn == true)
                {
                    SmtpServer.Host = "smtp.mail.yahoo.com.vn";
                    SmtpServer.Port = 587;
                }
                else
                {
                    SmtpServer.Host = "smtp.gmail.com";
                    SmtpServer.Port = 587;
                    SmtpServer.EnableSsl = true;
                }
                MailMessage mail = new MailMessage();

                mail.From = new MailAddress(theThongTinCongTy.Email, "Tilbakestill passord");
                mail.To.Add(this.txtEmail.Text.Trim());
                mail.Subject = "informasjon konto";
                mail.Body = "Takk! Nå kontoen er: \n";
                mail.Body += "E-post: " + theProfile.GetPropertyValue("Email").ToString() + "\n";
                mail.Body += "Passord:" + theUser.ResetPassword();
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.ReplyTo = new MailAddress(theThongTinCongTy.Email);
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                messageSendMail = "Send e feil " + ex.Message;
            }
            ltrMessage.Text = messageSendMail;
        }
    }
}